package com.baidu.yyb.noteCustomView;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.baidu.yyb.R;
import com.lt.utils.String.StringUtils;
import com.lt.utils.ViewUtils.ScreenUtils;
import com.lt.utils.ViewUtils.ViewUtils;
import org.jetbrains.annotations.Nullable;

/**
 * @author LiuTong
 */
public  class TitleView extends ConstraintLayout implements View.OnClickListener {
    private TextView tv_backBar, tv_title, tv_more, tv_statusBar;
    private View view;
    int topMargin = 0;
    private int titleTextSize;
    private boolean onlyShowTitle,onlyShowTwoAttr;
    private String titleName = "默认标题";
    private Activity activity;


    public TitleView(Context context) {
        super(context);
        this.activity = (Activity) context;
    }

    public TitleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.activity = (Activity) context;
        view = LayoutInflater.from(context).inflate(R.layout.title_view_layout, this);
        topMargin = ScreenUtils.getStatusBarHeight(context);
        if(ScreenUtils.hasNotchInScreen((Activity) context)){
            topMargin+=ScreenUtils.getNotchSizeAtHuawei(context);
        }
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.TitleViewAttrs);
        onlyShowTitle = typedArray.getBoolean(R.styleable.TitleViewAttrs_OnlyShowTitle,false);
        onlyShowTwoAttr = typedArray.getBoolean(R.styleable.TitleViewAttrs_OnlyShowTwoAttr,false);
        titleName = typedArray.getString(R.styleable.TitleViewAttrs_TitleName);
        titleTextSize = ScreenUtils.px2dip(context,typedArray.getDimensionPixelSize(R.styleable.TitleViewAttrs_TitleTextSize,ScreenUtils.dip2px(context,16)));
        /***
         * 手动回收
         */
        typedArray.recycle();
        if(StringUtils.isEmptyOrNull(titleName)){
            titleName = "默认标题";
        }

        initView();
        initEvent();
    }

    protected  void initEvent(){
        tv_backBar.setOnClickListener(this);
        tv_more.setOnClickListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private void initView() {
        tv_statusBar = view.findViewById(R.id.title_status_bar);
        tv_backBar = view.findViewById(R.id.title_back_bar);
        tv_title =  view.findViewById(R.id.title_name);
        tv_more =  view.findViewById(R.id.title_more);
        tv_statusBar.setHeight(topMargin);
        tv_title.setTextSize(titleTextSize);
        tv_title.setText(titleName);
        if(onlyShowTwoAttr){
            ViewUtils.setViewGone(tv_more);
        }else if(onlyShowTitle){
            ViewUtils.setViewGone(tv_more,tv_backBar);
        }
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.title_back_bar) {
//            ActivityManager.finishActivity(activity);
        } else if (id == R.id.title_more) {

        }
    }

    /***
     * 
     * @param type 1:表体  2：有按钮
     * @return
     */
    public TextView getBar(int type){
        switch (type){
            case 1:return tv_title;
            case 2:return tv_more;
            default:return tv_backBar;
        }
    }

    public void setTitle(String title){
        tv_title.setText(title);
    }



}
